home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / util / copool.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  5KB  |  144 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Manage a pool of routines using Python iterators.'''
  5. from gi.repository import GLib
  6.  
  7. class _Routine(object):
  8.     
  9.     def __init__(self, pool, func, funcid, priority, timeout, args, kwargs):
  10.         self.priority = priority
  11.         self.timeout = timeout
  12.         self._source_id = None
  13.         
  14.         def wrap(func, funcid, args, kwargs):
  15.             for value in func(*args, **kwargs):
  16.                 yield True
  17.             
  18.             pool.remove(funcid)
  19.             yield False
  20.  
  21.         self.source_func = wrap(func, funcid, args, kwargs).next
  22.  
  23.     
  24.     def paused(self):
  25.         '''If the routine is currently running'''
  26.         return self._source_id is None
  27.  
  28.     paused = property(paused)
  29.     
  30.     def step(self):
  31.         '''Raises StopIteration if the routine has nothing more to do'''
  32.         return self.source_func()
  33.  
  34.     
  35.     def resume(self):
  36.         '''Resume, if already running do nothing'''
  37.         if not self.paused:
  38.             return None
  39.         if None.timeout:
  40.             self._source_id = GLib.timeout_add(self.timeout, self.source_func, priority = self.priority)
  41.         else:
  42.             self._source_id = GLib.idle_add(self.source_func, priority = self.priority)
  43.  
  44.     
  45.     def pause(self):
  46.         '''Pause, if already paused, do nothing'''
  47.         if self.paused:
  48.             return None
  49.         None.source_remove(self._source_id)
  50.         self._source_id = None
  51.  
  52.  
  53.  
  54. class CoPool(object):
  55.     
  56.     def __init__(self):
  57.         self._CoPool__routines = { }
  58.  
  59.     
  60.     def add(self, func, *args, **kwargs):
  61.         '''Register a routine to run in GLib main loop.
  62.  
  63.         func should be a function that returns a Python iterator (e.g.
  64.         generator) that provides values until it should stop being called.
  65.  
  66.         Optional Keyword Arguments:
  67.         priority -- priority to run at (default GLib.PRIORITY_LOW)
  68.         funcid -- mutex/removal identifier for this function
  69.         timeout -- use timeout_add (with given timeout) instead of idle_add
  70.                    (in milliseconds)
  71.  
  72.         Only one function with the same funcid can be running at once.
  73.         Starting a new function with the same ID will stop the old one. If
  74.         no funcid is given, the function itself is used. The funcid must
  75.         be usable as a hash key.
  76.         '''
  77.         funcid = kwargs.pop('funcid', func)
  78.         if funcid in self._CoPool__routines:
  79.             remove(funcid)
  80.         priority = kwargs.pop('priority', GLib.PRIORITY_LOW)
  81.         timeout = kwargs.pop('timeout', None)
  82.         print_d('Added copool function %r with id %r' % (func, funcid))
  83.         routine = _Routine(self, func, funcid, priority, timeout, args, kwargs)
  84.         self._CoPool__routines[funcid] = routine
  85.         routine.resume()
  86.  
  87.     
  88.     def _get(self, funcid):
  89.         if funcid in self._CoPool__routines:
  90.             return self._CoPool__routines[funcid]
  91.         raise None('no pooled routine %r' % funcid)
  92.  
  93.     
  94.     def remove(self, funcid):
  95.         '''Stop a registered routine.'''
  96.         routine = self._get(funcid)
  97.         routine.pause()
  98.         del self._CoPool__routines[funcid]
  99.         print_d('Removed copool function id %r' % funcid)
  100.  
  101.     
  102.     def remove_all(self):
  103.         '''Stop all running routines.'''
  104.         for funcid in self._CoPool__routines.keys():
  105.             self.remove(funcid)
  106.         
  107.  
  108.     
  109.     def pause(self, funcid):
  110.         '''Temporarily pause a registered routine.'''
  111.         routine = self._get(funcid)
  112.         routine.pause()
  113.         print_d('Paused copool function id %r' % funcid)
  114.  
  115.     
  116.     def pause_all(self):
  117.         '''Temporarily pause all registered routines.'''
  118.         for funcid in self._CoPool__routines.keys():
  119.             self.pause(funcid)
  120.         
  121.  
  122.     
  123.     def resume(self, funcid):
  124.         '''Resume a paused routine.'''
  125.         routine = self._get(funcid)
  126.         routine.resume()
  127.         print_d('Resumed copool function id %r' % funcid)
  128.  
  129.     
  130.     def step(self, funcid):
  131.         '''Force this function to iterate once.'''
  132.         routine = self._get(funcid)
  133.         return routine.step()
  134.  
  135.  
  136. _copool = CoPool()
  137. add = _copool.add
  138. pause = _copool.pause
  139. pause_all = _copool.pause_all
  140. remove = _copool.remove
  141. remove_all = _copool.remove_all
  142. resume = _copool.resume
  143. step = _copool.step
  144.